/*_________________________
 *  NOPOPUPS.c -- Demostration No-Popup Nested Menu
 *				(c) Charles Petzold, 1998
 */
#include <Windows.h>

#define IDM_FILE     		40001
#define IDM_EDIT     		40002
#define IDM_FILE_NEW        40003
#define IDM_FILE_OPEN    	40004
#define IDM_FLIE_SAVE 		40005
#define IDM_FILE_SAVE_AS	40006
#define IDM_MAIN            40007
#define IDM_EDIT_UNDO       40008
#define IDM_EDIT_CUT        40009
#define IDM_EDIT_COPY       40010
#define IDM_EDIT_PASTE      40011
#define IDM_EDIT_CLEAR      40012

#define ID_TIMER 1

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

TCHAR szAppName[] = TEXT("NoPopUps");

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	// Load a custom icon from a PNG file
	// Windows does not support loading icons directly from PNG.
	// You need to convert the PNG to an .ico file first.
	// Suppose you have "icon.ico" in your project directory:
	// wndclass.hIcon = (HICON)LoadImage(NULL, TEXT("icon.ico"), IMAGE_ICON, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName,
		TEXT("No-Popup Nested Menu Demonstration"),
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		600, 600,
		NULL, NULL, hInstance, NULL);
	
    ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static HMENU hMenuMain, hMenuEdit, hMenuFile;
    HINSTANCE hInstance;

	switch (message)
	{
	case WM_CREATE:
	{
        hInstance = (HINSTANCE)GetWindowLong(hwnd, GWLP_HINSTANCE);
	    hMenuMain = LoadMenu(hInstance, TEXT("MenuMain"));
        hMenuFile = LoadMenu(hInstance, TEXT("MenuFile"));
        hMenuEdit = LoadMenu(hInstance, TEXT("MenuEdit"));
		SetMenu(hwnd, hMenuMain);
		return 0;
	}
	case WM_COMMAND:
	{
		switch (LOWORD(wParam))
        {
            case IDM_MAIN:{
                SetMenu(hwnd, hMenuMain);
                return 0;
            }
            case IDM_FILE:{
                SetMenu(hwnd, hMenuFile);
                return 0;
            }
            case IDM_EDIT:{
                SetMenu(hwnd, hMenuEdit);
                return 0;
            }
            case IDM_FILE_NEW:
            case IDM_FILE_OPEN: {
                MessageBeep(0);
                return 0;
            }
        }
		return 0;
	}
	case WM_TIMER: {
		MessageBeep(0);
		return 0;
	}
	case WM_DESTROY: {
        SetMenu(hwnd, hMenuMain);
        DestroyMenu(hMenuFile);
        DestroyMenu(hMenuEdit);
		PostQuitMessage(0);
		return 0;
	}
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}